import java.util.ArrayList;

/**
 * Created by yuchen on 16/5/10.
 */
public class dataSet {


    ArrayList data = new ArrayList();
    private int length = 0;
    private int areaWidth = 0;
    private int areaHeight = 0;



    private int width;
    private int height;
    private ArrayList area = new ArrayList();
    private ArrayList record = new ArrayList();
    private boolean isnoise = false;

    public dataSet(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public void flush() {
        this.length = this.data.size();
        int len = this.length;
        this.area = new ArrayList();
        this.record = new ArrayList();
        this.areaHeight = 0;
        this.areaWidth = 0;
        int i;
        int[] pot;
        int x, y;
        if (this.length == 0) {
            return;
        }
        pot = (int[])this.data.get(0);
        int maxW = pot[0], minW = pot[0], maxH = pot[1], minH = pot[1];
        int[] maxWs = pot, minWs = pot, maxHs = pot, minHs = pot;
        int maxWs_s = -1,minWs_s = -1,maxHs_s = -1,minHs_s = -1;
        for (i = 1; i < len; i++) {
            pot = (int[])this.data.get(i);
            x = pot[0];
            y = pot[1];
            if (x > maxW) {
                maxW = x;
                maxWs_s = i;
            } else if (x < minW) {
                minW = x;
                minWs_s = i;
            }
            if (y > maxH) {
                maxH = y;
                maxHs_s = i;
            } else if (y < minH) {
                minH = y;
                minHs_s = i;
            }
        }
        this.areaHeight = maxH - minH + 1;
        this.areaWidth = maxW - minW + 1;
        this.area.add(minW);
        this.area.add(maxW);
        this.area.add(minH);
        this.area.add(maxH);
        addListOrNum(maxWs, maxWs_s, this.record);
        addListOrNum(minWs, minWs_s, this.record);
        addListOrNum(maxHs, maxHs_s, this.record);
        addListOrNum(minWs, minWs_s, this.record);

        this.check();
    }

    public void addListOrNum(int[] arr, int checkNum,ArrayList list) {
        if (checkNum != -1) {
            list.add(checkNum);
        }else{
            list.add(arr);
        }
    }

    public void check() {
        int[] xcol = new int[this.width];
        int[] ycol = new int[this.height];
        int i, x, y;
        int[] pot = new int[2];
        for (i = 0; i < xcol.length; i++) {
            xcol[i] = 0;
        }
        for (i = 0; i < ycol.length; i++) {
            ycol[i] = 0;
        }
        int len = this.data.size();
        for (i = 0; i < len; i++) {
            pot = (int[])this.data.get(i);
            x = pot[0];
            y = pot[1];
            xcol[x]++;
            ycol[y]++;
        }

        int thx = width * 90 / 100;
        int thy = height * 90 / 100;
        for (i = 0; i < xcol.length; i++) {
            if (xcol[i] >= thx) {
                this.isnoise = true;
                return;
            }
        }
        for (i = 0; i < ycol.length; i++) {
            if (ycol[i] >= thy) {
                this.isnoise = true;
                return;
            }
        }
    }

    public int counthorizontal(int y,int startX,int endX){
        int calRowsNum = 0;
        int i;
        int[] pot;
        for (i = 0; i < this.length; i++) {
            pot = (int[])this.data.get(i);
            if (pot[1] == y && (pot[0] <= endX) && (pot[0] >= startX)) {
                calRowsNum++;
            }
        }
        return calRowsNum;
    };


    public ArrayList getData() {
        return data;
    }

    public int getLength() {
        return length;
    }

    public int getAreaWidth() {
        return areaWidth;
    }

    public int getAreaHeight() {
        return areaHeight;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public ArrayList getArea() {
        return area;
    }

    public ArrayList getRecord() {
        return record;
    }

    public boolean isnoise() {
        return isnoise;
    }
}
